import {defineStore} from 'pinia'
import {loginAPI} from "@/apis/user.js"
import {ref} from 'vue'
import {mergeCartApi} from "@/apis/cart.js";
import {useCartStore} from "@/stores/cartStroe.js";
export const useUserStore = defineStore('user', () => {
  const userDate = ref({})
  const getUser = async ({account, password}) => {
    const res = await loginAPI({account, password})
    userDate.value = res.result
    await mergeCartApi(useCartStore().cartList.map(item => {
      return{
        skuId: item.skuId,
        count: item.count,
        selected: item.selected
      }
    }))
  }
  const clearUser = () => {
    userDate.value = {}
  }
  return {
    userDate,
    getUser,
    clearUser
  }
}, {
    persist: true,//数据持久化
  })
